package tq.com.tqteacher.mine.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.mine.fragment.TQClassIntrFragment;
import tq.com.tqteacher.model.TQPublishType;

public class TQClassOverInfoAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private  AllOverInfo mAllOverInfo = new AllOverInfo();
    private ArrayList<SubjectInfo> mSubjectInfos = new ArrayList<>();

    public TQClassOverInfoAdapter(Context pContext){
        mContext = pContext;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pI == 0 && pViewHolder instanceof CommonViewHolder){
            ((CommonViewHolder)pViewHolder).bindData(mAllOverInfo);
        }else if(pViewHolder instanceof CourseTypeViewHold){
            SubjectInfo lSubjectInfo = mSubjectInfos.get(pI - 1);
            ((CourseTypeViewHold)pViewHolder).bind(lSubjectInfo);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        RecyclerView.ViewHolder lViewHolder = null;
        if(pI == 1){
            lViewHolder = CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_classoverinfo_1_layout, pViewGroup);
            ((CommonViewHolder) lViewHolder).setHolderBinder(new CommonViewHolder.IViewHolder() {
                @Override
                public void bind(View pView, Object data) {
                    TextView lTextView = pView.findViewById(R.id.count);
                    lTextView.setText(String.valueOf(mAllOverInfo.getAll()));

                    TextView lTextView1 = pView.findViewById(R.id.week);
                    lTextView1.setText(String.valueOf(mAllOverInfo.getWeek()));

                    TextView lTextView2 = pView.findViewById(R.id.day);
                    lTextView2.setText(String.valueOf(mAllOverInfo.getDay()));

                    TextView lTextView3 = pView.findViewById(R.id.week_add);
                    lTextView3.setText(String.valueOf(mAllOverInfo.getAdd()));
                }
            });
        }else{
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_classoverinfo_2_layout, pViewGroup, false);
            lViewHolder = new CourseTypeViewHold(lView);
        }
        return lViewHolder;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) return 1;
        return 0;
    }

    @Override
    public int getItemCount() {
        return 1 + mSubjectInfos.size();
    }

    public void updateOverInfo(JSONObject pObject){
        mAllOverInfo.fromjson(pObject);
        notifyItemChanged(0);
    }

    public  void updateSubject(JSONArray pJSONArray){
        if(pJSONArray == null) return;

        mSubjectInfos.clear();
        for (int i = 0; i < pJSONArray.length(); i++){
            try {
                JSONObject lJSONObject = pJSONArray.getJSONObject(i);
                SubjectInfo lSubjectInfo = new SubjectInfo();
                lSubjectInfo.fromJson(lJSONObject);

                mSubjectInfos.add(lSubjectInfo);
            }catch (JSONException pE){
                pE.printStackTrace();
            }
        }

        notifyItemRangeChanged(1, mSubjectInfos.size());
    }

    class CourseTypeViewHold extends RecyclerView.ViewHolder{
        private TextView mTextView;
        private GridView mGridView;

        private SubjectInfo mSubjectInfo;
        private DataAdapter mDataAdapter = new DataAdapter();

        public CourseTypeViewHold(View pView){
            super(pView);

            mGridView = pView.findViewById(R.id.gridView);
            mGridView.setAdapter(mDataAdapter);
            mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));

            mTextView = pView.findViewById(R.id.course);
        }

        public void bind(SubjectInfo pSubjectInfo){
            mSubjectInfo = pSubjectInfo;
            mTextView.setText(pSubjectInfo.getSubjectTitle());
            mDataAdapter.notifyDataSetChanged();
        }


        class DataAdapter extends BaseAdapter{
            @Override
            public Object getItem(int position) {
                return mSubjectInfo.mCateInfos.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public int getCount() {
                return mSubjectInfo != null ? mSubjectInfo.mCateInfos.size() : 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View lView = convertView;
                if(lView == null){
                    lView = LayoutInflater.from(mContext).inflate(R.layout.item_type_count_layout, parent, false);
                }

                SubjectInfo.CateInfo lInfo = mSubjectInfo.mCateInfos.get(position);
                TextView lTextView = lView.findViewById(R.id.text);
                lTextView.setText(String.format("%s %d", lInfo.getCateTitle(), lInfo.getCount()));
                return lView;
            }
        }
    }


    static public class AllOverInfo{
        private int all;
        private int week;
        private  int day;
        private int add;

        public void  fromjson(JSONObject pObject){
            if(pObject == null) return;

            all = pObject.optInt("total");
            week = pObject.optInt("wau");
            day = pObject.optInt("dau");
            add = pObject.optInt("wiu");
        }

        public int getAll() {
            return all;
        }

        public int getWeek() {
            return week;
        }

        public int getDay() {
            return day;
        }

        public int getAdd() {
            return add;
        }
    }

    static public  class SubjectInfo{
        private int subjectId;
        private ArrayList<CateInfo> mCateInfos = new ArrayList<>();


        public void fromJson(JSONObject pObject){
            if(pObject == null) return;

            subjectId = pObject.optInt("subject");
            JSONArray lArray = pObject.optJSONArray("cate");
            if(lArray == null) return;

            mCateInfos.clear();
            for (int i = 0; i < lArray.length(); i++){
                try {
                    JSONObject lObject = lArray.getJSONObject(i);

                    CateInfo lCateInfo = new CateInfo();
                    lCateInfo.fromJson(lObject);

                    mCateInfos.add(lCateInfo);
                }catch (JSONException pE){
                    pE.printStackTrace();
                }
            }
        }

        public String getSubjectTitle(){
            return TQSystemDefine.subjectTitle(subjectId);
        }

        public class CateInfo{
            private int cateId;
            private int count;

            public void fromJson(JSONObject pObject){
                if(pObject == null) return;

                cateId = pObject.optInt("cate");
                count = pObject.optInt("cnt");
            }

            public String getCateTitle(){
                return TQPublishType.title(cateId);
            }
            public int getCount(){return count;}
        }
    }
}
